package com.hundsun.yr.universal.p2p.ui.activity;

import android.animation.Animator;


import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.adapter.HolderAdapter;
import com.hundsun.yr.universal.library.adapter.ViewCreatorHelper;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSLabelTextView;
import com.hundsun.yr.universal.library.widget.HSLoadMoreListView;
import com.hundsun.yr.universal.library.widget.HSSwipeRefreshLayout;
import com.hundsun.yr.universal.p2p.bean.InvestEntity;

import com.hundsun.yr.universal.p2p.bean.LoginBeen;
import com.hundsun.yr.universal.p2p.bean.MessageBean;
import com.hundsun.yr.universal.p2p.bean.MessageSettingBean;
import com.hundsun.yr.universal.p2p.bean.ProductBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.ApiConstants2;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.BaseListEntityPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.BaseListView;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewPropertyAnimator;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import de.greenrobot.event.EventBus;

/**
 * Created by apple on 2017/12/4.
 */

public class MessageActivity extends BaseActivity implements BaseListView, ViewCreatorHelper.UpdateItem<MessageBean>
        , SwipeRefreshLayout.OnRefreshListener, HSLoadMoreListView.OnLoadMoreListener, AdapterView.OnItemClickListener, View.OnClickListener, HSDialog.HSDialogOnItemClickListener {
    private RequestPresenter mPresenter;
    private HSSwipeRefreshLayout mMessage_hsSwipeRefreshLayout = null;
    private HSLoadMoreListView mMessage_loadMoreListView = null;
    private HolderAdapter<MessageBean> mAdapter;
    private LayoutInflater mInflater;
    private RelativeLayout headerView;
    private String zwgt_data;
    private List<MessageBean> data = new ArrayList<>();
    private ImageView iv_arrow = null;
    private LinearLayout ll_title = null;
    private PopupWindow popWindow;
    private View title_view = null;
    private String type = "";
    private boolean isAmin = true;
    private HSDialog dialog_setting_message = null;
    private String product;
    private String activity;
    private String sale;
    private int lastVisiblePosition;
    private Long time;
    //    private List<ProductBeen> data = new ArrayList<>();

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_message;
    }

    @Override
    protected void initViewsAndEvents() {
        setToolbarTitle(R.string.instation_message);
        toolbar_menu.setText(R.string.all_red);
        toolbar_menu.setVisibility(View.VISIBLE);
        toolbar_menu.setOnClickListener(this);

        findView();
        iv_arrow.setVisibility(View.VISIBLE);
        ll_title.setOnClickListener(this);

        /**设置viewpager或者listView时候的一个帮组类**/
        ViewCreatorHelper<MessageBean> creatorHelper = new ViewCreatorHelper<MessageBean>(R.layout.item_message, this);
        mAdapter = new HolderAdapter<MessageBean>(mContext, creatorHelper.viewCreator());

        //将头添加进去
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        headerView = (RelativeLayout) mInflater.inflate(R.layout.item_header_10dp, mMessage_loadMoreListView, false);
        mMessage_loadMoreListView.addHeaderView(headerView);

        mMessage_loadMoreListView.setAdapter(mAdapter);
        mMessage_loadMoreListView.setOnItemClickListener(this);
        mMessage_hsSwipeRefreshLayout.setOnRefreshListener(this);
        mMessage_loadMoreListView.setOnLoadMoreListener(this);
        toolbar_menu.setOnClickListener(this);

        mPresenter = new BaseListEntityPresenterImpl(mContext, this, InvestEntity.BASEKEY);

        //首次进入的时候请求的网络
        mMessage_hsSwipeRefreshLayout.setRefreshing(true);

        loadingdata();
        zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map2 = new JSONObject();
        try {
            map2.put("a", "1");//随便传的无意义的参数，不然不能完成加密
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject jsonParams2 = CommTool.getJSONParams(map2, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
        mPresenter.loadingData(TaskConstants.TASK_NOTICELIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                TaskConstants.TASK_NOTICELIST, Constants.EVENT_BEGIN, jsonParams2, BaseRequestTool2.getHearder(mContext,
                TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_NOTICELIST, sharedPreferences.getShareProf(UserInfo.USER_NO),
                sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);


//        mAdapter.update(data);
//
//        if (!sharedPreferences.getBoolean(UserInfo.FIRST_SETTING_MESSAGE)){
//            dialog_setting_message = new HSDialog(getString(R.string.tips), getString(R.string.tips_setting_messasge),
//                    getString(R.string.wait), getString(R.string.go_), mContext,
//                    this).setCancelable(false);//.setOnDismissListener(this);
//            dialog_setting_message.show();
//        }
//
//        //提示用户设置
//        sharedPreferences.putBoolean(UserInfo.FIRST_SETTING_MESSAGE, true);
    }

    /**
     * 网络请求
     */
    private void loadingdata() {
        zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map = new JSONObject();
        try {
            map.put("type", type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);

        mPresenter.loadingData(TaskConstants.TASK_MSGLIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                TaskConstants.TASK_MSGLIST, Constants.EVENT_REFRESH_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_MSGLIST, sharedPreferences.getShareProf(UserInfo.USER_NO),
                sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);


    }

    private void findView() {
//        mMessage_hsSwipeRefreshLayout = findView(R.id.message_hsSwipeRefreshLayout);
        mMessage_loadMoreListView = findView(R.id.message_loadMoreListView);
        iv_arrow = findView(R.id.iv_arrow);
        ll_title = findView(R.id.ll_title);
        title_view = findView(R.id.title_view);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        mMessage_hsSwipeRefreshLayout = findView(R.id.message_hsSwipeRefreshLayout);
        return mMessage_hsSwipeRefreshLayout;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    public void onRefresh() {
        loadingdata();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isFastDoubleClick()) {
            return;
        }


        // TODO Auto-generated method stub
        if (position > data.size() || position <= 0) return;

        if (this.data.size() <= 0)
            return;

        data.get(position - 1).isread = "1";

        mAdapter.update(this.data);

        zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map = new JSONObject();
        try {
            map.put("id", data.get(position - 1).id);
            map.put("isread", "1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);

        mPresenter.loadingData(TaskConstants.TASK_MSGREADSET, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                TaskConstants.TASK_MSGREADSET, Constants.EVENT_REFRESH_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_MSGREADSET, sharedPreferences.getShareProf(UserInfo.USER_NO),
                sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);

        //将id带过去用于网络请求，显示详情页面的信息
        Bundle bundle = new Bundle();
        bundle.putSerializable(Extras.MESSAGE_DATA, data.get(position - 1));
        readyGo(MessageDetailActivity.class, bundle);
    }

    @Override
    public void refreshItem(View view, int position, MessageBean messageBean) {
        HSLabelTextView hstv_icon_type = findView(view, R.id.hstv_icon_type);
        TextView tv_title = findView(view, R.id.tv_title);
        TextView tv_isread = findView(view, R.id.tv_isread);
        TextView tv_time = findView(view, R.id.tv_time);
        TextView tv_content = findView(view, R.id.tv_content);

        switch (messageBean.type) {
            case "1":
                hstv_icon_type.setText(getString(R.string.activity));
                hstv_icon_type.setBorderColor(getResources().getColor(R.color.orange_red));
                hstv_icon_type.setBackColor(getResources().getColor(R.color.orange_red));
                break;
            case "2":
                hstv_icon_type.setText(getString(R.string.bonus));
                hstv_icon_type.setBorderColor(getResources().getColor(R.color.md_orange_300));
                hstv_icon_type.setBackColor(getResources().getColor(R.color.md_orange_300));
                break;
            case "3":
                hstv_icon_type.setText(getString(R.string.system));
                hstv_icon_type.setBorderColor(getResources().getColor(R.color.md_light_blue_500));
                hstv_icon_type.setBackColor(getResources().getColor(R.color.md_light_blue_500));
                break;
            case "4":
                hstv_icon_type.setText(getString(R.string.product));
                hstv_icon_type.setBorderColor(getResources().getColor(R.color.md_color_11111));
                hstv_icon_type.setBackColor(getResources().getColor(R.color.md_color_11111));
                break;
        }

        if (messageBean.isread.equals("1")) {
            tv_isread.setText(getString(R.string.read));
        } else {
            tv_isread.setText(getString(R.string.no_read));
        }

        tv_title.setText(messageBean.title);
        tv_time.setText(messageBean.ctime);
        tv_content.setText(messageBean.text);
    }

    @Override
    public <T> void refreshListData(List<T> result) {
        if (popWindow != null) {
            popWindow.dismiss();
        }
        mMessage_hsSwipeRefreshLayout.setRefreshing(false);
        mMessage_loadMoreListView.setLoadMoreEnabled(true);
        if (data.size() > 0) data.clear();
        if (result != null) {
            data.addAll((Collection<? extends MessageBean>) result);
            mMessage_loadMoreListView.setLoadMoreEnabled(true);
        }
        mAdapter.update(data);
    }

    @Override
    public <T> void addMoreListData(List<T> result) {
        mMessage_loadMoreListView.onLoadMoreComplete();

        if (data != null && data.size() > 0) {
//			pageNo++;
            data.addAll((Collection<? extends MessageBean>) result);
            mMessage_loadMoreListView.setLoadMoreEnabled(true);
//			loadMoreListView.setLoadMoreEnabled(result.size()==Constants.PAGESIZE_MIDDLE);
        } else {
            mMessage_loadMoreListView.setLoadMoreEnabled(false);
        }
        mAdapter.update(data);
    }

    @Override
    public <T> void onFailure(String task_name) {
        mMessage_hsSwipeRefreshLayout.setRefreshing(false);
        if (task_name.equals(TaskConstants.TASK_LOGINSILENT)){
            finish();
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_MSGREADALL:
                if (this.data.size() <= 0)
                    return;

                for (int i = 0; i < this.data.size(); i++) {
                    this.data.get(i).isread = "1";
                }
                mAdapter.update(this.data);
                break;

            case TaskConstants.TASK_NOTICELIST:
                if (this.data == null || this.data.size() == 0) {
                    showEmpty(0, mContext.getString(R.string.empty_msg), this);
                }
                //获取3个消息设置状态
                MessageSettingBean messageSettingBean = (MessageSettingBean) data;
                product = messageSettingBean.product;
                activity = messageSettingBean.activity;
                sale = messageSettingBean.sale;
                break;
            case TaskConstants.TASK_MSGREADSET:

                break;

            case TaskConstants.TASK_LOGINSILENT:
                LoginBeen loginBeen = (LoginBeen) data;
                if (loginBeen.unread != null && !loginBeen.unread.isEmpty()) {
                    sharedPreferences.putShareProf(UserInfo.UNREAD, loginBeen.unread);
                } else {
                    sharedPreferences.putShareProf(UserInfo.UNREAD, "0");
                }
                EventBus.getDefault().post(new EventCenter(Extras.MESSAGE));//登陆成功通知某些页面进行数据刷新
                finish();

                break;
        }
    }

    @Override
    public void onLoadMore() {
        lastVisiblePosition = mMessage_loadMoreListView.getLastVisiblePosition();
        //加载更多的时候，拿到最后一条的信息
        zwgt_data = BaseRequestTool2.getFormatedDateString();

        JSONObject map = new JSONObject();
        try {
            map.put("type", type);
            map.put("id", data.get(lastVisiblePosition - 2).id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);

        mPresenter.loadingData(TaskConstants.TASK_MSGLIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                TaskConstants.TASK_MSGLIST, Constants.EVENT_LOAD_MORE_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_MSGLIST, sharedPreferences.getShareProf(UserInfo.USER_NO),
                sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
    }


    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_title:
                if (isAmin) {
                    startAnim();
                    //弹出pop选择
                    showPopup(title_view);
                }

                break;

            case R.id.toolbar_menu:
                if (this.data.size() <= 0)
                    return;

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
//                try {
//                    map.put("type", type);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);

                mPresenter.loadingData(TaskConstants.TASK_MSGREADALL, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                        TaskConstants.TASK_MSGREADALL, Constants.EVENT_REFRESH_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                        TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_MSGREADALL, sharedPreferences.getShareProf(UserInfo.USER_NO),
                        sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
                break;
        }
    }

    private void startAnim() {
        if (isAmin) {
            ViewPropertyAnimator.animate(iv_arrow)
                    .rotationBy(180)
                    .setDuration(300)
                    .setListener(new com.nineoldandroids.animation.Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(com.nineoldandroids.animation.Animator animator) {
                            isAmin = false;
                        }

                        @Override
                        public void onAnimationEnd(com.nineoldandroids.animation.Animator animator) {
                            isAmin = true;
                        }

                        @Override
                        public void onAnimationCancel(com.nineoldandroids.animation.Animator animator) {

                        }

                        @Override
                        public void onAnimationRepeat(com.nineoldandroids.animation.Animator animator) {

                        }
                    })
                    .start();
        }
    }

    /**
     * 创建PopupWindow对象
     *
     * @param parent
     */
    @SuppressLint("InflateParams")
    private void showPopup(View parent) {
//        backgroundAlpha((float) 0.5);
        View view = null;
        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.pop_message_list, null);

        popWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);

        HSLoadMoreListView hslv_pop_message = findView(view, R.id.hslv_pop_message);
        View view_back_bottom = findView(view, R.id.view_back_bottom);
        view_back_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

        ViewCreatorHelper<String> creatorHelper = new ViewCreatorHelper<String>(R.layout.item_pop_message, new ViewCreatorHelper.UpdateItem<String>() {
            @Override
            public void refreshItem(View view, int position, String s) {
                TextView tv_message_type = findView(view, R.id.tv_message_type);
                tv_message_type.setText(s);
            }
        });
        HolderAdapter adapter = new HolderAdapter<String>(mContext, creatorHelper.viewCreator());
        hslv_pop_message.setAdapter(adapter);

        hslv_pop_message.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                switch (position) {
                    case 0:
                        type = "";
                        break;
                    case 1:
                        type = "3";
                        break;
                    case 2:
                        if (product != null && product.equals("1")) {
                            showToast(getString(R.string.no_this_function));
                        }
                        type = "4";
                        break;
                    case 3:
                        if (activity != null && activity.equals("1")) {
                            showToast(getString(R.string.no_this_function));
                        }
                        type = "1";
                        break;
                    case 4:
                        if (sale != null && sale.equals("1")) {
                            showToast(getString(R.string.no_this_function));
                        }
                        type = "2";
                        break;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("type", type);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);

                mPresenter.loadingData(TaskConstants.TASK_MSGLIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                        TaskConstants.TASK_MSGLIST, Constants.EVENT_REFRESH_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                        TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_MSGLIST, sharedPreferences.getShareProf(UserInfo.USER_NO),
                        sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
            }
        });

        ArrayList arrayList = new ArrayList();
        arrayList.add(getString(R.string.all_message));
        arrayList.add(getString(R.string.system_message));
        arrayList.add(getString(R.string.product_message));
        arrayList.add(getString(R.string.activity_message));
        arrayList.add(getString(R.string.discount_message));
        adapter.update(arrayList);

        // 设置背景，这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_66));
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
        popWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popWindow.setClippingEnabled(false);
        //软键盘不会挡着popupwindow
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popWindow.setTouchable(true);
        // 使其聚集 ，要想监听菜单里控件的事件就必须要调用此方法
        popWindow.setFocusable(true);
        // 设置允许在外点击消失
        popWindow.setOutsideTouchable(true);
//        //监听触屏事件
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View view, MotionEvent event) {
                return false;
            }
        });
        popWindow.showAtLocation(parent, Gravity.NO_GRAVITY, 0, mToolbar.getHeight());
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                startAnim();
            }
        });
    }

    @Override
    public void onItemClick(Object o, int position) {
        if ((HSDialog) o == dialog_setting_message) {
            if (position != -1) {
                readyGo(MessageSettingActivity.class);
            }
        }
    }


    /**
     * 自动登陆
     */
    private void automaticLogin() {
        if (sharedPreferences.getShareProf(UserInfo.TOKENID) != null && !sharedPreferences.getShareProf(UserInfo.TOKENID).isEmpty()) {
            zwgt_data = BaseRequestTool2.getFormatedDateString();
            JSONObject map = new JSONObject();
            try {
                map.put("token", sharedPreferences.getShareProf(UserInfo.TOKENID));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
            mPresenter.loadingData(TaskConstants.TASK_LOGINSILENT, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_index_test)
                    + TaskConstants.TASK_LOGINSILENT, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_index_test)
                    + TaskConstants.TASK_LOGINSILENT, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
        }
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            automaticLogin();
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            automaticLogin();
        }
        return super.onOptionsItemSelected(item);
    }
}
